<template>
  <div class="com-container">
    <div class="title" :style="{ fontSize: titleFontSize + 'px' }">
      <span>{{ "▎ " + title }}</span>
      <span
        class="iconfont title-icon"
        :style="{ fontSize: this.titleFontSize + 'px' }"
        @click="showChoice = !showChoice"
      >
        &#xe6eb;
      </span>
      <ul
        v-show="showChoice"
        :style="{ marginLeft: this.titleFontSize + 'px' }"
      >
        <li
          v-for="item in titleChange"
          :key="item.key"
          @click="changeType(item.key)"
        >
          {{ item.text }}
        </li>
      </ul>
    </div>
    <div class="com-chart" ref="trend_ref"></div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { getTrend } from "@/api";
export default {
  data() {
    return {
      echartsDom: null,
      trendDta: null,
      showType: "seller",
      showChoice: false,
      titleFontSize: 0,
    };
  },
  mounted() {
    this.getData();
    this.initEachat();
    this.screenAdapter();
  },
  computed: {
    ...mapState(["style"]),
    title() {
      if (!this.trendDta) {
        return "";
      } else {
        return this.trendDta[this.showType].title;
      }
    },
    titleChange() {
      if (this.trendDta) {
        return this.trendDta.type.filter((item) => item.key != this.showType);
      } else {
        return [];
      }
    },
  },
  watch: {
    style() {
      console.log("主题切换了");
      this.echartsDom.dispose(); // 销毁当前的图表
      this.initEachat(); // 重新以最新的主题名称初始化图表对象
      this.screenAdapter(); // 完成屏幕的适配
      this.upData(); // 更新图表的展示
    },
  },

  methods: {
    // 获取数据
    async getData() {
      this.trendDta = await getTrend();
      this.upData();
    },
    // 初始化Eachats元素
    initEachat() {
      this.echartsDom = this.$echarts.init(this.$refs.trend_ref, this.style); //第一个参数是dom元素，第二个是主题
      const initOption = {
        yAxis: {
          type: "value",
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
        },
        tooltip: {
          trigger: "axis",
        },
        grid: {
          left: "3%",
          top: "35%",
          right: "4%",
          bottom: "1%",
          containLabel: true,
        },
        legend: {
          left: "center",
          top: "15%",
          icon: "circle",
        },
      };
      this.echartsDom.setOption(initOption);
    },
    // 浏览器大小变化
    screenAdapter() {
      this.titleFontSize = (this.$refs.trend_ref.offsetWidth / 100) * 3.6;
      let adapterOption = {
        legend: {
          itemWidth: this.titleFontSize,
          itemHeight: this.titleFontSize,
          itemGap: this.titleFontSize,
          textStyle: {
            fontSize: this.titleFontSize / 2,
          },
        },
      };
      this.echartsDom.setOption(adapterOption);
      this.echartsDom.resize();
    },
    // 更新页面元素
    upData() {
      const colorArr1 = [
        "rgba(11, 168, 44, 0.5)",
        "rgba(44, 110, 255, 0.5)",
        "rgba(22, 242, 217, 0.5)",
        "rgba(254, 33, 30, 0.5)",
        "rgba(250, 105, 0, 0.5)",
      ];
      // 全透明的颜色值
      const colorArr2 = [
        "rgba(11, 168, 44, 0)",
        "rgba(44, 110, 255, 0)",
        "rgba(22, 242, 217, 0)",
        "rgba(254, 33, 30, 0)",
        "rgba(250, 105, 0, 0)",
      ];

      let yData = this.trendDta[this.showType].data.map((item, index) => {
        return {
          data: item.data,
          type: "line",
          name: item.name,
          stack: this.showType,
          areaStyle: {
            color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: colorArr1[index],
              }, // %0的颜色值
              {
                offset: 1,
                color: colorArr2[index],
              }, // 100%的颜色值
            ]),
          },
        };
      });
      let legendArr = this.trendDta[this.showType].data.map(
        (item) => item.name
      );
      const initOption = {
        xAxis: {
          data: this.trendDta.common.month,
        },
        // 图例
        legend: {
          data: legendArr,
        },
        series: yData,
      };
      this.echartsDom.setOption(initOption);
    },
    // 修改显示状态
    changeType(type) {
      this.showType = type;
      this.upData();
      this.showChoice = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.title {
  position: absolute;
  left: 20px;
  top: 20px;
  z-index: 10;
  .title-icon {
    margin-left: 10px;
    cursor: pointer;
  }
  .select-con {
    background: #222733;
  }
}
</style>